package com.chaopei.order_mng.service.impl;

import com.chaopei.order_mng.entity.OrderDetail;
import com.chaopei.order_mng.entity.OrderInfo;
import com.chaopei.order_mng.entity.OrderProductVo;
import com.chaopei.order_mng.mapper.OrderProductVoMapper;
import com.chaopei.order_mng.service.OrderDetailService;
import com.chaopei.order_mng.service.OrderInfoService;
import com.chaopei.order_mng.service.OrderProductVoService;
import com.chaopei.order_mng.utils.MyJson;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author Corday 天涯
 * @Date 2022/5/12 0:43
 */
@Service
public class OrderProductVoServiceImpl implements OrderProductVoService {
    @Resource
    private OrderInfoService orderInfoService;
    @Resource
    private OrderDetailService orderDetailService;
    @Resource
    private OrderProductVoMapper orderProductVoMapper;

    /**
     * 1.生成新实体对象
     *
     * @param jsonArray
     * @return
     */
    @Override
    public List<OrderProductVo> getOrderProductVos(String jsonArray) {
        return MyJson.jsonToVo(jsonArray);
    }

    /**
     * 2.vo->entity,动态拿到订单id
     *
     * @param list
     * @return
     */
    @Override
    public int saveOrderAndGetId(List<OrderProductVo> list) {
        //vo->entity生成vo实体的对象
        OrderProductVo temp = list.get(0);
        //生成订单基本实体对象
        OrderInfo orderInfo = new OrderInfo();
        //设置uid
        orderInfo.setUid(temp.getUid());
        //设置ordertime
        orderInfo.setOrdertime(temp.getOrdertime());
        //id,uid,status,ordertime,自动获取订单id,订单状态status默认为未付款
        //保存订单数据并返回订单id,订单详情表要使用
        int oid = orderInfoService.saveOrderInfo(orderInfo);
        return oid;
    }

    /**
     * 3.拿到有订单id的集合对象orderDetailList,它的数据要从其它表中获取
     *
     * @param list
     * @return vo->entity
     * OrderDetail:oid,pid,num
     */
    @Override
    public String saveOrderInfoAndOrderDetail(List<OrderProductVo> list) {
        //拿到订间详情表为的数据,传给OrderDetailService中的saveOrderDetail(List<OrderDetail> orderDetails)
        List<OrderDetail> orderDetails = new ArrayList<>();
        String info = "订单基本表添加失败";
        //(1)保存订单基本信息表
        int oid = saveOrderAndGetId(list);
        if (oid > 0) {
            info = "订单基本表添加成功";
            for (OrderProductVo temp : list) {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setOid(oid);
                orderDetail.setPid(temp.getPid());
                orderDetail.setNum(temp.getNum());
                orderDetails.add(orderDetail);
            }
        }
        ////(2)保存到商品详情表
        String msg = orderDetailService.saveOrderDetail(orderDetails);
        info += ":" + msg;
        return info;
    }

    /**
     * @param uid
     * @return
     * @OrderInfoMapper and OrderProductVoMapper协同完成
     * 4.封装map<oid,vos>集合对象,能否直接通过数据查询生成map
     */
    @Override
    public Map<Integer, List<OrderProductVo>> queryOrderProductVo(int uid) {
        //存订单与订单中的所有购买的商品
        Map<Integer, List<OrderProductVo>> map = new LinkedHashMap<>();
        List<OrderProductVo> vos = new ArrayList<>();
        //查询用户所有oid
        List<Integer> oids = orderInfoService.queryOidsByUid(uid);
        //创建map对象
        for (Integer oid : oids) {
            vos = orderProductVoMapper.selectOrderProduct(oid);
            map.put(oid,vos);
        }
        return map;
    }

}
